import DialogStore from ".";
import { action, observable, computed } from 'mobx';
import { Button, Modal, message } from 'antd';
import { WeaTools, WeaLocaleProvider } from 'ecCom'
import API from "../../apis/customCode";
const getLabel = WeaLocaleProvider.getLabel;

export default class AceEditorDialogStore extends DialogStore {

    @observable dsType = "0";
    @observable dataSource = [];
    @observable dbTables = [];
    @observable quickSearchValue = '';
    @observable tableLoading = false;
    @observable heihgt = 0;
    @observable formulaList;
    @observable currentCodeContent;
    @observable currentId;
    @observable currentPageId;
    @observable defaultContent = '<script type="text/javascript">\n/*\n* ' + getLabel(389599, "请在下面编写JS代码") + "\n*/\n\n<\/script>";
    constructor(props) {
        super();
        this.style = { width: 620 };
        this.title = "代码块";
        this.buttons = [<Button type="primary" onClick={this.save}>{getLabel(30986, '保存')}</Button>,
        <Button onClick={this.hide} type="primary">{getLabel(31129, '取消')}</Button>]
    }

    @action openCodeEditor = (currentFormula) => {
        if (!this.currentPageId) {
            message.error("请先保存模板，再创建代码块");
            return;
        }
        console.log()
        this.queryById(this.currentPageId);
        this.open();
    }

    @action setValue = (cusCode) => {
        console.log(cusCode)
        this.currentCodeContent = this.defaultContent;
        if (cusCode) {
            this.currentCodeContent = cusCode ? WeaTools.Base64.decode(cusCode) : this.defaultContent;
        }
    }

    @action queryById = (id) => {
        API.queryCustomCodeById(id).then(this.setValue);
    }

    @action save = () => {
        API.saveCustomCode({
            id: this.currentId,
            content: WeaTools.Base64.encode(this.currentCodeContent),
            pageid: this.currentPageId
        }).then((values) => {
            this.hide();
            message.success(getLabel('502231', '保存成功'))
        });

    }

    @action onChange = (value) => {
        this.currentCodeContent = value;
    }

}